package mock.servicesdirectory.contacts.web.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import mock.servicesdirectory.common.conts.ContactModelConts;
import mock.servicesdirectory.common.entities.Contact;
import mock.servicesdirectory.common.entities.Users;
import mock.servicesdirectory.contacts.business.businessinterface.ContactBo;
import mock.servicesdirectory.contacts.business.businessinterface.UserBo;

import com.opensymphony.xwork2.ActionSupport;

public class EditContactAction extends ActionSupport implements ServletRequestAware{
		
	private static final long serialVersionUID = 1L;
	
	/*
	 * Property used for get request parameter
	 */
	private HttpServletRequest request;

	/*
	 * Property of that action corresponse with jsp page
	 */
	private int contactId=-1;
	private int userId;
	private String firstName;
	private String surName;	
	private String managerName;
	private String contactType;
	private String knownAs;
	private String bestContactMethod;
	private String officePhone;
	private String mobilePhone;
	private String sthomePhone;
	private String email;
	private String jobRole;
	private String workbase;
	private String jobTitle;
	private boolean isActive;
	
	/*
	 * property Business object that use to perform anything about business rule and
	 * retrieve database
	 */
	private ContactBo contactBo;
	private UserBo userBo;
	
	/**
	 * Method used to process saveOrUpdateContact 
	 * @return
	 */
	public String saveOrUpdateContact(){
		Contact contact = new Contact();
		if(contactId != -1){
			contact.setContactId(contactId);
		}
		
		Users users = userBo.getUser(userId);
		
		contact.setFirstName(firstName);
		contact.setSurName(surName);
		contact.setKnownAs(knownAs);
		contact.setOfficePhone(officePhone);
		contact.setMobilePhone(mobilePhone);
		contact.setSthomePhone(sthomePhone);
		contact.setEmail(email);
		contact.setUsers(users);
		contact.setContactType(contactType);
		contact.setBestContactMethod(bestContactMethod);
		contact.setJobRole(jobRole);
		contact.setWorkbase(workbase);
		contact.setJobTitle(jobTitle);
		contact.setIsActive(isActive);
		contactBo.saveOrupdateContact(contact);
		
		return SUCCESS;		
	}		

	/**
	 * Method used to get infomation from database through business object
	 * The information will be fill into textfield of the jsp page.
	 * User can change from this information
	 * @return
	 */
	public String prepareContact(){
		String strContactId = request.getParameter(ContactModelConts.CONTACT_ID_PARAMETER);
		if(strContactId != null && !strContactId.isEmpty()){
			contactId = Integer.parseInt(strContactId);
			Contact contact = contactBo.getContact(contactId);
			firstName = contact.getFirstName();
			surName = contact.getSurName();
			knownAs = contact.getKnownAs();
			officePhone = contact.getOfficePhone();
			mobilePhone = contact.getMobilePhone();
			sthomePhone = contact.getSthomePhone();
			email = contact.getEmail();
			userId = contact.getUsers().getUserId();
			managerName = contact.getUsers().getAccount();
			contactType = contact.getContactType();
			bestContactMethod = contact.getBestContactMethod();
			jobRole = contact.getJobRole();
			workbase = contact.getWorkbase();
			jobTitle = contact.getJobTitle();
			isActive = contact.isIsActive();
		}
		
		return SUCCESS;
	}
	
	/**
	 * @param contactId the contactId to set
	 */
	public void setContactId(int contactId) {
		this.contactId = contactId;
	}

	/**
	 * @return the contactId
	 */
	public int getContactId() {
		return contactId;
	}

	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param surName the surName to set
	 */
	public void setSurName(String surName) {
		this.surName = surName;
	}

	/**
	 * @return the surName
	 */
	public String getSurName() {
		return surName;
	}

	/**
	 * @param contactType the contactType to set
	 */
	public void setContactType(String contactType) {
		this.contactType = contactType;
	}

	/**
	 * @return the contactType
	 */
	public String getContactType() {
		return contactType;
	}

	/**
	 * @param managerName the managerName to set
	 */
	public void setManagerName(String managerName) {
		this.managerName = managerName;
	}

	/**
	 * @return the managerName
	 */
	public String getManagerName() {
		return managerName;
	}

	/**
	 * @param bestContactMethod the bestContactMethod to set
	 */
	public void setBestContactMethod(String bestContactMethod) {
		this.bestContactMethod = bestContactMethod;
	}

	/**
	 * @return the bestContactMethod
	 */
	public String getBestContactMethod() {
		return bestContactMethod;
	}

	/**
	 * @param knownAs the knownAs to set
	 */
	public void setKnownAs(String knownAs) {
		this.knownAs = knownAs;
	}

	/**
	 * @return the knownAs
	 */
	public String getKnownAs() {
		return knownAs;
	}

	/**
	 * @param officePhone the officePhone to set
	 */
	public void setOfficePhone(String officePhone) {
		this.officePhone = officePhone;
	}

	/**
	 * @return the officePhone
	 */
	public String getOfficePhone() {
		return officePhone;
	}

	/**
	 * @param mobilePhone the mobilePhone to set
	 */
	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	/**
	 * @return the mobilePhone
	 */
	public String getMobilePhone() {
		return mobilePhone;
	}

	/**
	 * @param sthomePhone the sthomePhone to set
	 */
	public void setSthomePhone(String sthomePhone) {
		this.sthomePhone = sthomePhone;
	}

	/**
	 * @return the sthomePhone
	 */
	public String getSthomePhone() {
		return sthomePhone;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param jobRole the jobRole to set
	 */
	public void setJobRole(String jobRole) {
		this.jobRole = jobRole;
	}

	/**
	 * @return the jobRole
	 */
	public String getJobRole() {
		return jobRole;
	}

	/**
	 * @param workbase the workbase to set
	 */
	public void setWorkbase(String workbase) {
		this.workbase = workbase;
	}

	/**
	 * @return the workbase
	 */
	public String getWorkbase() {
		return workbase;
	}

	/**
	 * @param jobTitle the jobTitle to set
	 */
	public void setJobTitle(String jobTitle) {
		this.jobTitle = jobTitle;
	}

	/**
	 * @return the jobTitle
	 */
	public String getJobTitle() {
		return jobTitle;
	}

	/**
	 * @param isActive the isActive to set
	 */
	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	/**
	 * @return the isActive
	 */
	public boolean isActive() {
		return isActive;
	}

	/**
	 * @param userId the userId to set
	 */
	public void setUserId(int userId) {
		this.userId = userId;
	}

	/**
	 * @return the userId
	 */
	public int getUserId() {
		return userId;
	}

	/**
	 * @param contactBo the contactBo to set
	 */
	public void setContactBo(ContactBo contactBo) {
		this.contactBo = contactBo;
	}

	/**
	 * @return the contactBo
	 */
	public ContactBo getContactBo() {
		return contactBo;
	}
	
	public HttpServletRequest getServletRequest() {
		return request;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	/**
	 * @param userBo the userBo to set
	 */
	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	/**
	 * @return the userBo
	 */
	public UserBo getUserBo() {
		return userBo;
	}
}
